<?php
/***************
* @Project	Warabi
* @Author	Feifei
* @Date		04/05/2009
***************/

/**this module analyses
   *xml (http request body) and 
   *generates xml(http response body)
   */

class wbXmlParser{
	
	/** the xml source ****/
	var $xmlsource = "";

	/** the xml handler **/
	var $xmlhandler;

	/**Constructor
	* @param $xmlsrc input the xml string
	**/
	function wbXmlParser($xmlsrc){
		$this->xmlhandler = new DOMDocument;
		$this->xmlhandle->validateOnParse = true;
		try {
		   $this->xmlhandler->load($xmlsrc);
		}
		catch(DOMException $e) { 
			return false;
		}
	}	

	/** set the xml source
	* @param $xmlsrc set the xml string
	**/
	function setSource($xmlsrc){
		$this->xmlsource = $xmlsrc;
	}

	
	/** get the value and property for a collection of same nodename
	* @param $key the node name
	* @return array(array($val,$property array),...,array($val,$property array))
	**/
	function getNodesResult($key , $attr=false){
		$nodes = $this->xmlhandler->getElementsByTagName($key);
		$collection = array();
		foreach($nodes as $node){
			 $value = $node->nodeValue;
			 $attr_res = array();
			if($attr){
				foreach($attr as $a){
					$attr_res[$a] = $node->getAttribute($a);
				}
			}
			$collection[] = array($value , $attr_res);
		}
		return $collection;
	}

	function freeHandler(){
		
	}
}

/**$handler = new XmlParser("<?xml version='1.0' encoding='utf-8'?><entry><invite><usr id='11' time='xx'>jackie</usr><lang>chinese</lang></invite><invite><usr id='11' time='xx'>feifei</usr><lang>english</lang></invite></entry>");
if(!$handler) return;
$usr = $handler->getNodesResult("usr" , array("id",'time'));
var_dump($usr);
**/
?>